﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using FlanProjectData.Helpers;

namespace FlanProjectData.GameObjects
{
    public class Quest
    {

        #region Quest State Enum

        enum QuestStateEnum
        {
            NOTSTARTED,
            STARTED,
            COMPLETE,
        }

        #endregion

        #region Attributs

        [ContentSerializerIgnore]
        private static Logger logger = new Logger("Quest", Log_Type.DEBUG);

		private int idQuest;
		public int IdQuest { get; set; }
		public String QuestName { get; set; }
		public List<String> DialogList { get; set; } 
		[ContentSerializerIgnore]
		public int ProgressIndexDialog { get; set; }
		public List<int> Checkpoint { get; set; }
		[ContentSerializerIgnore]
		public int ProgressIndexCheckpoint { get; set; }
		[ContentSerializerIgnore]
		private QuestStateEnum state = QuestStateEnum.NOTSTARTED;

		public int IdNextQuest { get; set; }

        #endregion

        #region Ctor

        public Quest()
        {
            this.DialogList = new List<String>();
            this.Checkpoint = new List<int>();
            ProgressIndexCheckpoint = 0;
            ProgressIndexDialog = 0;
        }

        #endregion

        public String GetQuestInfomation()
        {
            try
            {
                return DialogList[this.ProgressIndexDialog];
            }
            catch (IndexOutOfRangeException)
            {
                logger.Warn("No text to be displayed");
                return "";
            }
        }

        public bool EndOfQuest()
        {
            // Last element is "Quest Finished" or smthg like that
            return ((DialogList.Count - ProgressIndexDialog) == 1);
        }

		public void nextStep()
		{
			this.ProgressIndexCheckpoint++;
			this.ProgressIndexDialog++;
			logger.Debug("[" + this.QuestName + "]" + " is continuing. CP=" + this.ProgressIndexCheckpoint + "/" + (this.Checkpoint.Count()));
		}

        public bool isCollision(int collisionNumber)
        {
            bool coderet = false;
            try
            {
                if (this.Checkpoint[this.ProgressIndexCheckpoint] == collisionNumber && this.ProgressIndexCheckpoint < this.Checkpoint.Count) coderet = true;
                return coderet;
            }
            catch (IndexOutOfRangeException)
            {
                logger.Warn("progresse index checkpoint out of range : " + ProgressIndexCheckpoint + " > " + this.Checkpoint.Count);
                return coderet;
            }
            catch (ArgumentOutOfRangeException)
            {
                logger.Warn("progresse index checkpoint out of range : " + ProgressIndexCheckpoint + " > " + this.Checkpoint.Count);
                return coderet;
            }
        }

        public void startQuest()
        {
            this.state = QuestStateEnum.STARTED;
        }

        //public void AddDialog(String d)
        //{
        //    this.dialogList.Add(d);
        //}

        //public void AddDialogList(List<String> lstD)
        //{
        //    foreach (String s in lstD)
        //    {
        //        this.dialogList.Add(s);
        //    }
        //}
    }
}
